package com.earnings.okhttputils.utils.ui.activity.user;

import android.content.Context;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.TextView;

import com.earnings.R;
import com.earnings.okhttputils.utils.OkHttp.OkHttpUtils;
import com.earnings.okhttputils.utils.OkHttp.utils.ToastUtils;
import com.earnings.okhttputils.utils.UrlConstant.HttpUrl;
import com.earnings.okhttputils.utils.bean.UserCaibaoTurnBalanceData;
import com.earnings.okhttputils.utils.god.GodLeftHandBaseActivity;
import com.earnings.okhttputils.utils.god.Listener.GodOnClickListener;
import com.earnings.okhttputils.utils.https.HttpMap;
import com.earnings.okhttputils.utils.https.HttpObjectCallback;
import com.earnings.okhttputils.utils.ui.activity.common.StatusPriceActivity;
import com.earnings.okhttputils.utils.ui.dialog.InfoDialog;
import com.earnings.okhttputils.utils.ui.dialog.phoenix.keybottondialog.PswKeyBoardBottomDialog;
import com.earnings.okhttputils.utils.utils.CommonUtil;

import java.util.Map;


/*
财币页面-转入余额
 */

public class UserTreasureTurnPriceActivity extends GodLeftHandBaseActivity implements View.OnClickListener, GodOnClickListener {


    private EditText price;//金额
    private TextView allin,info;//全部转入
    @Override
    protected int onCreateLayoutId() {
        return R.layout.activity_user_treasure_turnprice;
    }

    @Override
    protected void init() {
        setTitle("转入余额");
        price= (EditText) findViewById( R.id.price );
        findViewById(R.id.sumbit).setOnClickListener(this);
      //  findViewById(R.id.info).setOnClickListener(this);
        price.setHint("可转入余额的财币："+CommonUtil.getUser().getWithdrawal() );
        setText( R.id.balance, "可转入余额的财币："+CommonUtil.getUser().getWithdrawal() );
        setText( R.id.infotext, "累计"+CommonUtil.getUser().getFreeze()+"财币不可转入余额优先用于消费支付" );

        setOnClickListener( this,R.id.sumbit,R.id.allin);
        price.addTextChangedListener( new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
           if(price.getText().toString().equals("")){
               return;}

               float nowprice=Float.parseFloat( price.getText().toString() );
                if(nowprice>Float.parseFloat(CommonUtil.getUser().getWithdrawal()))
                    price.setText( CommonUtil.getUser().getWithdrawal() );

            }


        } );
    }


public boolean checkPrice(){
    String money=price.getText().toString();
    if(money.equals("")){
        ToastUtils.showToast(getContext(),"请输入转入的余额");
        return false;}

        return true;
}

    @Override
    public void OnClickListener(View view) {
        int i = view.getId();
        if (i == R.id.sumbit) {
            if(checkPrice())sumbit();

        } else if (i == R.id.info) {
            InfoDialog info = new InfoDialog( getContext(), "1.财币转入余额必须是100的整数倍\n2.提取财币操作将要扣除10%的服务费" );
            info.show();

        }else if (i == R.id.allin) {
            price.setText( CommonUtil.getUser().getRich());

        }
    }

    public void sumbit(){

        InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.hideSoftInputFromWindow(price.getWindowToken(), 0); //强制隐藏键盘
        final PswKeyBoardBottomDialog pswdialog=new PswKeyBoardBottomDialog(this,price.getText().toString());
        pswdialog.setTitle("使用招财汇宝财币支付");
        pswdialog.setOnPswKeyBoardResultListener( new PswKeyBoardBottomDialog.OnPswKeyBoardResultListener() {
            @Override
            public void getPswResult(String mResult) {

                Map map=new HttpMap( getContext() );
                map.put( "amount",price.getText().toString() );
                map.put( "paypwd",mResult);
                OkHttpUtils.post().params( map ).url( HttpUrl.USER_TURN_BALANCE_URL ).build().execute( new HttpObjectCallback<UserCaibaoTurnBalanceData>(getContext()) {
                    @Override
                    public void onSuccess(UserCaibaoTurnBalanceData object, String msg) {
                        pswdialog.showSus();
                        bundleData.putString( "title", "转入余额" );
                        bundleData.putString( "status", msg );
                        bundleData.putString( "info", "" );
                        bundleData.putString( "price", price.getText().toString() );
                        skipActivity( StatusPriceActivity.class );
                        finish();
                      //  dismissProgress();
                    }

                    @Override
                    public void onFail(int code, String msg) {
                        pswdialog.showFail(msg);
                     //   dismissProgress();
                    }
                } );
            }
        } );

        pswdialog.show();
    }
}
